<?php
	class baseData{
		protected $db;
		protected $user = "root";
		protected $pass = "";
		protected $host = "localhost";
		protected $dbname = "mypham";
		protected $dsn = 'mysql:host=localhost;dbname=mypham';
		
		function __construct() {
			try
			{
				$user = "root";
				$pass = "";
				$this->db = new PDO($this->dsn, $user, $pass);
			}
			catch(PDOException $e)
			{
				die("<b>Error:</b> ".$e->getMessage());
			}	
		}
		
		function connect()
		{
			try
			{
				$db = new PDO($this->dsn, $this->user, $this->pass);
				$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
				return $db;
			}
			catch(PDOException $e)
			{
				die("<b>Error:</b> ".$e->getMessage());
			}	
		}
		
		function InsertData($table,$array)
		{
			try
			{
				$db = $this->connect();
				if(!is_array($array))
					return false;
				$field = "`".implode(",",array_keys($array))."`";
				$value = implode(",",$array);
				$st = $db->prepare(" INSERT INTO {$table}(".$field.") VALUES('".$value."') ");
				$st->execute();			
			}
			catch(PDOException $e)
			{
				die("<b>Error:</b> ".$e->getMessage());
			}	
		}
		
		function UpdateData($table,$array,$where)
		{
			try
			{
				$db = $this->connect();
				if(!is_array($array))
					return false;
				if(empty($where))
					return false;
					
				$query = "";
				$str = "";
				foreach($array as $key => $value)
				{
					$query .= $str."`".$key."` = '".$value."'";
					$str = ",";	
				}
				
				$st = $db->prepare(" UPDATE {$table} SET {$query} WHERE {$where} ");
				$st->execute();
			}
			catch(PDOException $e)
			{
				die("<b>Error:</b> ".$e->getMessage());
			}		
		}
		
		function DeleteData($table,$value)
		{
			try
			{
				$db = $this->connect();
				if(empty($value))
					return false;
				$st = $db->prepare(" DELETE FROM {$table} WHERE {$value} ");
				$st->execute();
			}
			catch(PDOException $e)
			{
				die("<b>Error:</b> ".$e->getMessage());
			}	
		}
		
		function GetData($table,$select,$where="",$order_by="",$limit = "")
		{
			try
			{
				$db = $this->connect();
				$s = "*";$w = "";$o = ""; $l = "";
				if($select != '')
					$s = implode(",",array_values($select));
				if(!empty($where))
					$w = " WHERE {$where}";
				if(!empty($order_by))
					$o = " ORDER BY {$order_by} ";
				if(!empty($limit))
					$l = " LIMIT {$limit} ";
				$st = $db->prepare(" SELECT {$s} FROM {$table} {$w} {$o} {$l} ");
				$st->execute();
				return $st->fetchAll();
			}
			catch(PDOException $e)
			{
				die("<b>Error:</b> ".$e->getMessage());
			}			
		}
		
		function GetJson($array)
		{
			try
			{
				return json_encode($array);
			}
			catch(PDOException $e)
			{
				die("<b>Error:</b> ".$e->getMessage());
			}
		}
		
	}
	
	class process extends baseData
	{
		function __construct(){
			parent::__construct();	
		}
		function exe($query)
		{
			$db = $this->connect();
			$st = $db->prepare($query);
			$st->execute();
		}
		
		function check($table,$array,$select="*")
		{
			try
			{
				$db = $this->connect();
				$where = "";
				if(is_array($array))
				{
					$str = ""; $where = " WHERE  ";
					foreach($array as $k => $v)
					{
						if($k == 'password')
							$v = sha1($v);
						$where .= $str.$k." = '".$v."'";
						$str = " AND ";
					}	
				}
				$st = $db->prepare(" SELECT {$select} FROM {$table} $where ");
				$st->execute();
				return $st->rowCount();
			}
			catch(PDOException $e)
			{
				die("<b>Error:</b> ".$e->getMessage());
			}
		}
		
	}
?>